﻿var insertMode;
var dollarPrice;
var poundPrice;
var importPrice;

insertMode = true;

$(function () {

    $.datepicker.setDefaults($.datepicker.regional["es"]);

    $("#_lastPurchaseDate").datepicker({
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        yearRange: "-100:+10",
        onSelect: function (dateText, inst) {
            var date = $(this).datepicker('getDate'),
            day = date.getDate(),
            month = date.getMonth() + 1,
            year = date.getFullYear();
            $("#LastPurchaseDate").val(month + "-" + day + "-" + year)
        }
    });

    $("#_lastSellDate").datepicker({
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        yearRange: "-100:+10",
        onSelect: function (dateText, inst) {
            var date = $(this).datepicker('getDate'),
            day = date.getDate(),
            month = date.getMonth() + 1,
            year = date.getFullYear();
            $("#LastSellDate").val(month + "-" + day + "-" + year)
        }
    });

    $("#PoundPrice").live("change", function () {

        try {
            FormatValue(this);
            $("#DollarPrice").val(this.value * poundPrice);
            $("#DollarPrice").trigger('change');
        }
        catch (err) {
            this.value = "";
        }
    });

    $("#DollarPrice").live("change", function () {
        try {
            FormatValue(this);
            $("#ArgPrice").val(this.value * dollarPrice);
            $("#ArgPrice").trigger('change');
        }
        catch (err) {
            this.value = "";
        }
    });

    $("#ArgPrice").live("change", function () {
        try {
            FormatValue(this);
        }
        catch (err) {
            this.value = "";
        }
        $("#ProfitPercentage").trigger('change');
    });

    $("#ProfitPercentage").live("change", function () {
        try {
            FormatValue(this);

        }
        catch (err) {
            this.value = "";
        }

        if (this.value != "") {
            var publicPrice = $("#ArgPrice").val() * (100 + ($("#ProfitPercentage").val()) * 1) / 100;
            publicPrice = publicPrice * (100 + (importPrice) * 1) / 100;
            publicPrice = Math.round((publicPrice * 100)) / 100;
            $("#PublicPrice").val(publicPrice);
            $("#PublicPrice").trigger('change');
        }
    });

    $("#PublicPrice").live("change", function () {
        try {
            FormatValue(this);

        }
        catch (err) {
            this.value = "";
        }


        if (this.value != "") {
            var ivaPrice = $("#PublicPrice").val() * 1.21;
            ivaPrice = Math.round((ivaPrice * 100)) / 100;
            $("#IVAPrice").val(ivaPrice);
            $("#IVAPrice").trigger('change');
        }
    });

    $("#IVAPrice").live("change", function () {
        try {
            FormatValue(this);
        }
        catch (err) {
            this.value = "";
        }
    });

    Custom.init();
});

function FormatValue(obj) {
    //cambiar comas por puntos
    if (obj.value.indexOf(",") > -1) {
        obj.value = obj.value.replace(",", ".");
        $(obj).trigger('change');
        return false;
    }

    //redondear a segundo decimal
    var newValue = Math.round((obj.value * 100)) / 100;
    if (obj.value != newValue) {
        obj.value = newValue;
        $(obj).trigger('change');
        return false;
    }
}

function validateDecimal(value) {
    var RE = /^(\d{1,7}|\d{1,7}\.\d{0,2})$/
    if (RE.test(value)) {
        return true;
    } else {
        return false;
    }
}

function beginSubmit(content) {
    $("#ErrorMessage").html("");
    if (validateForm()) {
        $("#Save").attr("disabled", true);
        //        var day = $("#LastPurchaseDate").datepicker('getDate').getDate();
        //        var month = $("#LastPurchaseDate").datepicker('getDate').getMonth() + 1;
        //        var year = $("#LastPurchaseDate").datepicker('getDate').getFullYear();  
        //        var newdate = month + "/" + day + "/" + year;
        //        alert(newdate);
        //        $("#LastPurchaseDate").val(newdate);

        return true;
    }
    return false;
}

function submitCompleted(content) {
    var response;

    response = content.get_object().Data;

    if (response) {
        closePopupAndRefresh(insertMode);
    }
    else {
        $("#Save").removeAttr("disabled");
        alert("error");
    }
}

function validateForm() {
    if (DoValidations(true)) {
        return true;
    }
    return false;
}

function subtmitError(content) {
    alert(content);
}